use sea_orm_migration::{prelude::*, schema::*};

#[derive(DeriveMigrationName)]
pub struct Migration;

#[async_trait::async_trait]
impl MigrationTrait for Migration {
    async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager
            .create_table(
                Table::create()
                    .table(Todo::Table)
                    .if_not_exists()
                    .col(pk_auto(Todo::Id))
                    .col(string(Todo::Title))
                    .col(integer(Todo::Status))
                    .col(date_time(Todo::CreatedAt))
                    .col(date_time_null(Todo::UpdatedAt))
                    .to_owned(),
            )
            .await?;

        let insert = sea_query::Query::insert()
            .into_table(Todo::Table)
            .columns([Todo::Title, Todo::Status, Todo::CreatedAt])
            .values_panic(["haha".into(), 1.into(), chrono::Local::now().into()])
            .to_owned();
        manager.exec_stmt(insert).await?;

        Ok(())
    }

    async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager
            .drop_table(Table::drop().table(Todo::Table).to_owned())
            .await?;

        Ok(())
    }
}

#[derive(DeriveIden)]
enum Todo {
    Table,
    Id,
    Title,
    Status,
    CreatedAt,
    UpdatedAt,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_todo() {
        assert_eq!(Todo::Table.to_string(), "todo");
        assert_eq!(Todo::Id.to_string(), "id");
        assert_eq!(Todo::Title.to_string(), "title");
        assert_eq!(Todo::CreatedAt.to_string(), "create_at");
        assert_eq!(Todo::UpdatedAt.to_string(), "update_at");
    }
}
